package com.gitee.alpha.ie;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.gitee.alpha.ie.handler.ExcelComment;
import com.gitee.alpha.ie.vo.CommentVo;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <br>
 *
 * @author 冰羽
 * @version 2.2.0
 */
public class ExcelHelper extends AbstractExcelHelper {

  @Override
  protected List<ExcelField> findFields(List<Field> fields) {
    List<ExcelField> excelFields = Lists.newArrayList();
    List<Field> list = fieldsSortByPosition(fields);
    for (int i = 0; i < list.size(); i++) {
      Field field = list.get(i);
      ApiModelProperty apiModelProperty = field.getAnnotation(ApiModelProperty.class);

      ExcelField excelField =
          ExcelField.builder()
              .original(field)
              .title(apiModelProperty.value())
              // 此处重新编序
              .cellIndex(i)
              .required(apiModelProperty.required())
              .converter(getConverter(field.getType()))
              .build();

      // 初始化dropDownValues
      Map<Integer, String[]> dropDownValues = buildDropDownValues(excelField.getCellIndex(), field);

      excelField.setDropDownValues(dropDownValues);

      excelFields.add(excelField);
    }

    return excelFields;
  }

  @Override
  public List<CommentVo> buildCommentVos(
      List<ExcelField> excelFields, int rowIndex, int rowOffset, int colOffset) {
    List<CommentVo> vos = Lists.newArrayList();

    excelFields.forEach(
        item -> {
          ExcelComment comment = item.getOriginal().getAnnotation(ExcelComment.class);
          if (comment != null && StrUtil.isNotEmpty(comment.text())) {
            CommentVo vo =
                new CommentVo(rowIndex, item.getCellIndex(), rowOffset, colOffset, comment.text());
            vos.add(vo);
          }
        });

    return vos;
  }

  private List<Field> fieldsSortByPosition(List<Field> fields) {
    // 防止在编码时候由于ctrl + c/v导致的position相同的情况, 需要排序后重新编码
    List<Field> list =
        fields.stream()
            .filter(item -> item.getAnnotation(ApiModelProperty.class) != null)
            .collect(Collectors.toList());

    CollectionUtil.sort(
        list,
        new Comparator<Field>() {
          @Override
          public int compare(Field o1, Field o2) {
            return o1.getAnnotation(ApiModelProperty.class).position()
                - o2.getAnnotation(ApiModelProperty.class).position();
          }
        });

    return list;
  }
}

// // ~ Spring Data Jpa

// import com.gitee.alpha.Constant;
// import cn.com.zhaoweiping.framework.repository.support.ColumnType;
// import cn.hutool.core.util.ArrayUtil;
// import cn.hutool.core.util.EnumUtil;
// import com.google.common.collect.Maps;
// import java.util.LinkedHashMap;
// import javax.persistence.EnumType;
// import javax.persistence.Enumerated;

//  @Override
//  protected Map<Integer, String[]> buildDropDownValues(int colIndex, Field field) {
//    Map<Integer, String[]> dropDownValues = Maps.newHashMap();
//    Class clazz = field.getType();
//    if (clazz.isEnum()) {
//      LinkedHashMap<String, Object> enums = EnumUtil.getEnumMap(clazz);
//      List<String> list = Lists.newArrayList();
//      Enumerated enumerated = field.getAnnotation(Enumerated.class);
//      if (CollectionUtil.isNotEmpty(enums)) {
//        enums.forEach(
//            (k, v) -> {
//              String val = String.valueOf(k);
//              if (v instanceof Constant && enumerated != null) {
//                Constant cv = (Constant) v;
//                if (enumerated.value() == EnumType.STRING) {
//                  val = cv.getLabel();
//                } else if (enumerated.value() == EnumType.ORDINAL) {
//                  val = val;
//                }
//              } // else if () // 其他枚举类型
//
//              list.add(val);
//            });
//
//        String[] values = ArrayUtil.toArray(list, String.class);
//        if (ArrayUtil.isNotEmpty(values)) {
//          dropDownValues.put(colIndex, values);
//        }
//      }
//    } else if (ColumnType.BOOLEAN.getBasicType().equals(clazz.getName())
//        || ColumnType.BOOLEAN.getJavaClass().getName().equals(clazz.getName())) {
//      dropDownValues.put(colIndex, new String[] {"TRUE", "FALSE"});
//    }
//
//    return dropDownValues;
//  }
